#include "svpwm.h"

pwm_t PWM_Calc;


void Invter_Init(void)
{
   PWM_Calc.PwmTBPRD=PWM_PERIOD;
   PWM_Calc.UCMP = 0;
   PWM_Calc.VCMP =0;
   PWM_Calc.WCMP =0;

   PWM_Calc.Sector=0;
   PWM_Calc.LastSector=0;
}

void Invter_Calc(void)
{
	int8_t A,B,C;
	int16_t V1,V2,V3; 
	int16_t T0,T1,T2;
	int32_t temp1,temp2;
	#if(BUS_Detect_En ==1)
	temp2=(SQRT3_Q13<<10);
	temp1=(int16_t)(temp2/SysPara.DcBusFiltered);
	#else
	temp1=1024;
	#endif
	
		V1=(PWM_Calc.VbetaPU_q14*temp1)>>10;

    //0.866*alpha - 0.5*beta
    V2=(14188*PWM_Calc.ValfaPU_q14)>>14;
    V2-=(PWM_Calc.VbetaPU_q14>>1);
	  V2=(V2*temp1)>>10;
 
	//-0.866*alpha - 0.5*beta
    V3=(-(14188 * PWM_Calc.ValfaPU_q14))>>14;
    V3-=(PWM_Calc.VbetaPU_q14>>1);
		V3=(V3*temp1)>>10;

		A = (V1 > 0)?1:0;
		B = (V2 > 0)?1:0;
		C = (V3 > 0)?1:0;

    PWM_Calc.Sector=(C<<2)+(B<<1)+A;

   switch(PWM_Calc.Sector)
	{
		case 1:
		
		T1 = -V2;
		T2 = -V3;
		
		break;
		
		case 2:
		
			T1 = -V3;
			T2 = -V1;
		
		break;
		
		case 3:
		
			T1 = V2;
			T2 = V1;
		
		break;
		
		case 4:
		
			T1 = -V1;
			T2 = -V2;
		
		break;
		
		case 5:
		
			T1 =  V1;
			T2 =  V3;
		
		break;		
		
		case 6:
		
			T1 = V3;
			T2 = V2;
		
		break;
		
		default:
		
			T1 = 0;
			T2 = 0;
		
		break;
	}
//过调制
	if((T1 + T2) > (PWM_Calc.PwmTBPRD))
	{
#if(OVER_MOD_EN == 1)
		if(
			(T1 > T2) && \
			(T1 > PWM_Calc.PwmTBPRD)
		  )  //过调制二区
		{
			T1 = PWM_Calc.PwmTBPRD;
			T2 = 0;
		}
		else if(T2 > PWM_Calc.PwmTBPRD)
		{
			T1 = 0;
			T2 = PWM_Calc.PwmTBPRD;
		}
		else
		{
			T1 = (int16_t)((T1 * PWM_Calc.PwmTBPRD) / (T1 + T2));
			T2 = (PWM_Calc.PwmTBPRD) - T1;
		}
#else	
	temp2 = T1 * PWM_Calc.PwmTBPRD;
	temp1 = T1 + T2;
	
	T1 = (int16_t)((T1 * PWM_Calc.PwmTBPRD) / (T1 + T2));

	T2 = (PWM_Calc.PwmTBPRD) - T1;
#endif
	}
	T0 = (PWM_Calc.PwmTBPRD - T1 - T2)>>1;
	if(T0 < MIN_PULSE_WIDTH)
	{
		T0 = MIN_PULSE_WIDTH;
	}
	

    switch(PWM_Calc.Sector)
	{
		case 1:
		
			PWM_Calc.UCMP = T0+T1;
			PWM_Calc.VCMP = T0;
			PWM_Calc.WCMP = T1+T2+T0;
		
		break;
		
		case 2:
		
			PWM_Calc.UCMP = T0;
			PWM_Calc.VCMP = T0+T1+T2;
			PWM_Calc.WCMP = T1+T0;
		
		break;
		
		case 3:
		
			PWM_Calc.UCMP = T0;
			PWM_Calc.VCMP = T1+T0;
			PWM_Calc.WCMP = T1+T2+T0;
		
		break;
		
		case 4:
		
			PWM_Calc.UCMP = T1+T2+T0;
			PWM_Calc.VCMP = T1+T0;
			PWM_Calc.WCMP = T0;
		
		break;
		
		case 5:
		
			PWM_Calc.UCMP = T1+T2+T0;
			PWM_Calc.VCMP = T0; 
			PWM_Calc.WCMP = T1+T0;
		
		break;
		
		case 6:
		
			PWM_Calc.UCMP = T1+T0;
			PWM_Calc.VCMP = T1+T2+T0;
			PWM_Calc.WCMP = T0; 
		
		break;
		
		default:
		
			PWM_Calc.UCMP = T0;
			PWM_Calc.VCMP = T0;
			PWM_Calc.WCMP = T0;
		
		break;	
	}
	#if(DeadTimeComEN==1)
	deadtime_compensation();
	#endif
}

void Update_PWM(void)
{

    TIM1->CCR1=PWM_Calc.UCMP;
    TIM1->CCR2=PWM_Calc.VCMP;
    TIM1->CCR3=PWM_Calc.WCMP;
	
	  TIM1->CCR4 = 3; 
}

void PWMOutputs_ENABLE(void)
{

	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);  
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); 


	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);  
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); 

	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);  
	HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3); 
}

void PWMOutputs_DISABLE(void)
{

	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);  
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);   


	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2);      
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);  
		

	HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_3);     
	HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);  
}

